//
//  OrderSeatsViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-1-27.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "OrderSeatsViewController.h"

@interface OrderSeatsViewController ()

@property (strong, nonatomic) NSMutableArray *photoList;

@end

@implementation OrderSeatsViewController

- (id)initWithOrderId:(NSString *)orderId andSeatType:(int)seatType
{
    self = [super initWithNibClass:[OrderSeatsViewController class]];
    if (self) {
        _orderId = orderId;
        _seatType = seatType;
        _photoList = [[NSMutableArray alloc] initWithCapacity:5];
        _dataSource = [[OrderSeatsDataSource alloc] initWithVC:self];
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    
    [_dataSource fetchSeats];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"预定的座位"];
    
    _seatsTable.delegate = _dataSource;
    _seatsTable.dataSource = _dataSource;
}

- (void)tapImageWithIndex:(NSInteger)index
{
    if (_photoList.count == 0) {
        for (NSDictionary *dic in _dataSource.seatList) {
            NSString *url = [dic objectForKey:@"image"];
            NSString *seatName = [dic objectForKey:@"zwname"];
            NSString *renshu = [dic objectForKey:@"renshu"];
            NSString *size = [dic objectForKey:@"size"];
            NSString *sheshi = [dic objectForKey:@"sheshi"];
            NSString *description = [NSString stringWithFormat:@"名称:%@   人数:%@人   面积:%@平方米   设施:%@",seatName,renshu,size,sheshi];
            
            MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
            photo.caption = description;
            [_photoList addObject:photo];
        }
    }
    
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.displayActionButton = NO;
    browser.displayNavArrows = YES;
    browser.displaySelectionButtons = NO;
    browser.alwaysShowControls = NO;
    browser.wantsFullScreenLayout = YES;
    browser.zoomPhotosToFill = YES;
    browser.enableGrid = NO;
    browser.startOnGrid = NO;
    [browser setCurrentPhotoIndex:index];
    
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:nc animated:YES];
}

#pragma mark - MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photoList.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photoList.count)
        return [_photoList objectAtIndex:index];
    return nil;
}

@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

@implementation OrderSeatsDataSource

- (id)initWithVC:(id)Controller
{
    self = [super init];
    if (self) {
        self.Controller = Controller;
        self.seatList = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)fetchSeats
{
    NSString *url = [NSString stringWithFormat:API_MYORDER_SEATS, _Controller.orderId];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    if([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    NSDictionary *data = [json objectForKey:@"data"];
    self.seatList = [data objectForKey:@"zuowei"];
    
    [self.Controller.seatsTable reloadData];
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _Controller.seatType == 1 ? 90 : 52;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _seatList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_Controller.seatType == 1) {
        static NSString *CellId = @"BoxCell";
        BoxCell *cell = [_Controller createCellWithNib:CellId tableview:tableView];
        
        NSMutableDictionary *dictionary = [_seatList objectAtIndex:indexPath.row];
        [cell setContent:dictionary andIndex:indexPath.row];
        cell.delegate = _Controller;
        cell.orderBtn.hidden = YES;
        
        return cell;
    }
    else {
        NSString *CellId = @"HallCell";
        HallCell *cell = [_Controller createCellWithNib:CellId tableview:tableView];
        
        NSMutableDictionary *dictionary = [_seatList objectAtIndex:indexPath.row];
        [cell setContent:dictionary];
        cell.orderBtn.hidden = YES;
        
        return cell;
    }
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
